<?php namespace Phpcmf\Controllers\Admin;

class Home extends \Phpcmf\App
{

    public function index() {

        $modules = \Phpcmf\Service::L('cache')->get('module-'.SITE_ID.'-content');
        if (!$modules) {
            $this->_admin_msg(0, dr_lang('未安装任何内容模块'));
        }

        $module = [];
        foreach ($modules as $t) {
            $module[$t['dirname']] = \Phpcmf\Service::L('cache')->get('module-'.SITE_ID.'-'.$t['dirname']);
        }

        \Phpcmf\Service::V()->assign([
            'module' => $module,
            'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
                [
                    '模块字段转移' => [APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/index', 'fa fa-cog'],
                ]
            ),
        ]);
        \Phpcmf\Service::V()->display('config.html');

    }

    // 生成主表代码
    public function z_index() {

        $dir = dr_safe_filename($_GET['dir']);
        $fid = dr_safe_filename($_GET['fid']);
        $fname = dr_safe_filename($_GET['fname']);

        $code = '';
        $field = '`'.$fname.'` MEDIUMTEXT NULL DEFAULT NULL';


        $sql = \Phpcmf\Service::M()->db->query("SHOW CREATE TABLE `".\Phpcmf\Service::M()->dbprefix(SITE_ID.'_'.$dir)."_data_0`")->getRowArray();
        $sql = str_replace(
            array($sql['Table'], 'CREATE TABLE'),
            array('{tablename}', 'CREATE TABLE IF NOT EXISTS'),
            $sql['Create Table']
        );
        $sql = explode('SQL_FINECMS_EOL', trim(str_replace(array(PHP_EOL, chr(13), chr(10), '\n\t'), 'SQL_FINECMS_EOL', $sql)));
        foreach ($sql as $rt) {
            if (strpos($rt, '`'.$fname.'`') !== false) {
                $field = trim($rt, ',');
            }
        }
        if (strpos($field, 'CREATE TABLE') === false) {
            // 生成失败
            $code.= 'ALTER TABLE `'.\Phpcmf\Service::M()->dbprefix(SITE_ID.'_'.$dir).'` ADD '.$field.';'.PHP_EOL.PHP_EOL;
            for ($i = 0; $i < 200; $i ++) {
                if (!\Phpcmf\Service::M()->db->query("SHOW TABLES LIKE '".\Phpcmf\Service::M()->dbprefix(SITE_ID.'_'.$dir).'_data_'.$i."'")->getRowArray()) {
                    break;
                }
                $code.= 'UPDATE `'.\Phpcmf\Service::M()->dbprefix(SITE_ID.'_'.$dir).'` a, `'.\Phpcmf\Service::M()->dbprefix(SITE_ID.'_'.$dir).'_data_'.$i.'` b SET a.`'.$fname.'` = b.`'.$fname.'` WHERE a.id = b.id;'.PHP_EOL.PHP_EOL;
                $code.= 'ALTER TABLE `'.\Phpcmf\Service::M()->dbprefix(SITE_ID.'_'.$dir).'_data_'.$i.'` DROP `'.$fname.'`;'.PHP_EOL.PHP_EOL;
            }
            $code.= 'UPDATE `'.\Phpcmf\Service::M()->dbprefix('field').'`  SET `ismain` = 1 WHERE id = '.$fid.';'.PHP_EOL.PHP_EOL;
        } else {
            $code = '数据库代码生成失败，请联系插件作者为你手动写代码（windows服务器很容易失败）';
        }


        \Phpcmf\Service::V()->assign([
            'code' => $code,
        ]);
        \Phpcmf\Service::V()->display('code.html');
        exit;
    }

    // 生成附表代码
    public function f_index() {

        $dir = dr_safe_filename($_GET['dir']);
        $fid = dr_safe_filename($_GET['fid']);
        $fname = dr_safe_filename($_GET['fname']);

        $field = '`'.$fname.'` MEDIUMTEXT NULL DEFAULT NULL';

        $sql = \Phpcmf\Service::M()->db->query("SHOW CREATE TABLE `".\Phpcmf\Service::M()->dbprefix(SITE_ID.'_'.$dir)."`")->getRowArray();
        $sql = str_replace(
            array($sql['Table'], 'CREATE TABLE'),
            array('{tablename}', 'CREATE TABLE IF NOT EXISTS'),
            $sql['Create Table']
        );
        $sql = explode(PHP_EOL, $sql);
        foreach ($sql as $rt) {
            if (strpos($rt, '`'.$fname.'`') !== false) {
                $field = trim($rt, ',');
            }
        }

        $code = '';
        for ($i = 0; $i < 200; $i ++) {
            if (!\Phpcmf\Service::M()->db->query("SHOW TABLES LIKE '".\Phpcmf\Service::M()->dbprefix(SITE_ID.'_'.$dir).'_data_'.$i."'")->getRowArray()) {
                break;
            }
            $code.= 'ALTER TABLE `'.\Phpcmf\Service::M()->dbprefix(SITE_ID.'_'.$dir).'_data_'.$i.'` ADD '.$field.';'.PHP_EOL.PHP_EOL;
            $code.= 'UPDATE `'.\Phpcmf\Service::M()->dbprefix(SITE_ID.'_'.$dir).'` a, `'.\Phpcmf\Service::M()->dbprefix(SITE_ID.'_'.$dir).'_data_'.$i.'` b SET b.`'.$fname.'` = a.`'.$fname.'` WHERE a.id = b.id;'.PHP_EOL.PHP_EOL;

        }
        $code.= 'UPDATE `'.\Phpcmf\Service::M()->dbprefix('field').'`  SET `ismain` = 0 WHERE id = '.$fid.';'.PHP_EOL.PHP_EOL;
        $code.= 'ALTER TABLE `'.\Phpcmf\Service::M()->dbprefix(SITE_ID.'_'.$dir).'` DROP `'.$fname.'`;'.PHP_EOL.PHP_EOL;


        \Phpcmf\Service::V()->assign([
            'code' => $code,
        ]);
        \Phpcmf\Service::V()->display('code.html');
        exit;
    }

}
